<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="申请编号" prop="inventoryNo">
				<el-input v-model="queryParams.inventoryNo" placeholder="请输入申请编号" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="盘点人员" prop="inventoryBy">
				<el-input v-model="queryParams.inventoryBy" placeholder="请输入盘点人员" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="盘点状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择盘点状态" clearable>
					<el-option v-for="dict in inventory_order_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="制定日期" style="width: 308px;">
				<el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
					start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="primary"-->
			<!--          plain-->
			<!--          icon="Plus"-->
			<!--          @click="handleAdd"-->
			<!--          v-hasPermi="['archive:ArchiveStocktakingTask:add']"-->
			<!--        >新增</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="success"-->
			<!--          plain-->
			<!--          icon="Edit"-->
			<!--          :disabled="single"-->
			<!--          @click="handleUpdate"-->
			<!--          v-hasPermi="['archive:ArchiveStocktakingTask:edit']"-->
			<!--        >修改</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="danger"-->
			<!--          plain-->
			<!--          icon="Delete"-->
			<!--          :disabled="multiple"-->
			<!--          @click="handleDelete"-->
			<!--          v-hasPermi="['archive:ArchiveStocktakingTask:remove']"-->
			<!--        >删除</el-button>-->
			<!--      </el-col>-->
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:ArchiveStocktakingTask:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>
		<!-- 盘点任务列表 -->
		<el-table v-loading="loading" :data="ArchiveStocktakingTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<!--      <el-table-column label="序号" align="center" prop="taskId" />-->
			<el-table-column label="申请编号" align="center" prop="inventoryNo" />
			<el-table-column label="盘点任务内容" align="center" prop="inventoryContent" />
			<el-table-column label="盘点任务制定日期" align="center" prop="inventoryDate" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.inventoryDate) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="盘点人员" align="center" prop="inventoryBy" />
			<el-table-column label="档案柜数量" align="center" prop="cabinetNum" />
			<el-table-column label="档案盒数量" align="center" prop="boxNum" />
			<el-table-column label="盘点状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="inventory_order_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="推送状态" align="center" prop="pushStatus">
				<template #default="scope">
					<dict-tag :options="push_status" :value="scope.row.pushStatus" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)"
						v-hasPermi="['archive:ArchiveStocktakingTask:edit']">详情</el-button>&nbsp;
					<el-button type="text" icon="Upload" @click="finishTask(scope.row, scope.index)"
						v-hasPermi="['archive:ArchiveStocktakingTask:edit']" v-if="scope.row.status =='02'">盘点完成
					</el-button>
					&nbsp;
					<el-link type="success" icon="Upload" v-if="scope.row.status == '03'"
						v-hasPermi="['archive:ArchiveStocktakingTask:edit']"
						@click="reportTask(scope.row, scope.index)">盘点上报</el-link>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 查看详情 -->
		<el-dialog title="盘点任务详情" v-model="openscan" width="90%" append-to-body>
			<el-form ref="ArchiveStocktakingTaskRef" :model="form" label-width="120px">
				<el-row>
					<el-col :span="24">
						<el-row>
							<el-col :span="6">
								<el-form-item label="申请编号" prop="inventoryNo">
									<el-input v-model="form.inventoryNo" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="盘点任务内容" prop="inventoryContent">
									<el-input v-model="form.inventoryContent" style="width:230px;"
										disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="制定日期" prop="inventoryDate">
									<el-input v-model="form.inventoryDate" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>

							<el-col :span="6">
								<el-form-item label="盘点人员" prop="inventoryBy">
									<el-input v-model="form.inventoryBy" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="档案柜数量" prop="cabinetNum">
									<el-input v-model="form.cabinetNum" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
							<el-col :span="6">
								<el-form-item label="档案盒数量" prop="boxNum">
									<el-input v-model="form.boxNum" style="width:230px;" disabled="disabled" />
								</el-form-item>
							</el-col>
						</el-row>
					</el-col>
				</el-row>
				<el-row :gutter="10" class="mb8">
					<el-col :span="1.5">
						<el-button type="primary" icon="Plus" @click="handleAddArchiveBox"
							v-hasPermi="['archive:ArchiveStocktakingTask:edit']" v-if="form.status == '02'">新增盘盈档案盒
						</el-button>
					</el-col>
				</el-row>
				<el-table :data="archiveBoxList" @row-click="handleBoxRowClick" height="200" border
					style="border:1px solid #dcdfe6;min-height: 200px;max-height: 200px;" :highlight-current-row="true"
					ref="box_table">
					<el-table-column label="序号" align='center' width="50">
						<template #default="scope">
							{{ scope.$index + 1 }}
						</template>
					</el-table-column>
					<el-table-column label="档案盒号" prop="boxNo" align="center" width="170" />
					<el-table-column label="档案区编号" prop="archAreaNo" align="center" width="200">
						<template #default="scope">
							<el-select v-if="canEdit(scope.row)" v-model="scope.row.archAreaNo" placeholder="请选择档案区编号"
								@change="changeArea(scope.row)" style="width:160px;">
								<el-option v-for="item in areaOptions" :key="item.archAreaNo" :label="item.archAreaNo"
									:value="item.archAreaNo">
								</el-option>
							</el-select>
							<span v-else>{{ scope.row.archAreaNo }}</span>
						</template>
					</el-table-column>
					<el-table-column label="档案柜编号" prop="colNo" align="center" width="200">
						<template #default="scope">
							<!-- 当盘盈时，显示选择框 -->
							<el-select v-if="canEdit(scope.row)" v-model="scope.row.colNo" placeholder="请选择档案柜编号"
								style="width:160px;">
								<el-option v-for="item in cabinetOptions" :key="item.colNo" :label="item.colNo"
									:value="item.colNo">
								</el-option>
							</el-select>
							<!-- 其他情况下显示 colNo -->
							<span v-else>{{ scope.row.colNo }}</span>
						</template>
					</el-table-column>
					<el-table-column label="库位号" prop="houseLocationNo" align="center" width="200">
						<template #default="scope">
							<el-input v-model="scope.row.houseLocationNo" style="width:160px;" maxlength="20"
								v-if="canEdit(scope.row)"></el-input>
							<span v-else>{{ scope.row.houseLocationNo }}</span>
						</template>
					</el-table-column>
					<el-table-column label="盘点结果" align="center" width="170">
						<template #default="scope">
							<el-select style="width:130px;" v-model="scope.row.inventoryResult"
								v-if="canEdit2(scope.row)">
								<el-option v-for="dict in filterInventoryResult(scope.row)" :key="dict.value"
									:label="dict.label" :value="dict.value">
								</el-option>
							</el-select>
							<dict-tag :options="inventory_result" :value="scope.row.inventoryResult" v-else />
						</template>
					</el-table-column>
					<el-table-column label="错位状态" align="center" prop="missStatus" width="170">
						<template #default="scope">
							<el-select style="width:130px;" v-model="scope.row.missStatus" v-if="canEdit2(scope.row)">
								<el-option v-for="dict in miss_status" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="miss_status" :value="scope.row.missStatus" v-else />
						</template>
					</el-table-column>
					<el-table-column label="实际库位号" prop="actualHouseLocationNo" align="center" width="170">
						<template #default="scope">
							<el-input v-model="scope.row.actualHouseLocationNo" style="width:130px;" maxlength="20"
								v-if="canEdit2(scope.row)"></el-input>
							<span v-else>{{ scope.row.actualHouseLocationNo }}</span>
						</template>
					</el-table-column>
					<el-table-column label="新增盘盈盒" prop="addStatus" align="center" width="150">
						<template #default="scope">
							{{ scope.row.addStatus == ADD_STATUS_Y ? '是' : '否' }}
						</template>
					</el-table-column>
					<el-table-column label="操作" align="center" fixed="right" width="220">
						<template #default="scope">
							<el-link type="success" icon="edit" v-hasPermi="['archive:ArchiveStocktakingTask:edit']"
								v-if="form.status == '02'" @click="handleBoxEdit(scope.row, scope.index)">修改</el-link>
							&nbsp;
							<el-button type="text" icon="View" v-hasPermi="['archive:ArchiveStocktakingTask:edit']"
								@click="hideExit(scope.row)" v-if="canEdit(scope.row)">取消</el-button>
							&nbsp;
							<el-button type="text" icon="View" @click="handleViewDetail(scope.row.id)"
								v-hasPermi="['archive:ArchiveStocktakingTask:edit']" v-else>查看</el-button>
						</template>
					</el-table-column>
				</el-table>
				<div style="height: 10px;"></div>
				<el-row :gutter="10" class="mb8">
					<el-col :span="1.5">
						<el-button type="primary" icon="Plus" @click="handleAddArcvhive"
							v-hasPermi="['archive:ArchiveStocktakingTask:edit']" v-if="form.status == '02'">新增盘盈档案
						</el-button>
					</el-col>
				</el-row>
				<!-- 档案列表 -->
				<el-table :data="archiveCheckList" ref="archive_table" @selection-change="handleSelDAChange"
					style="border:1px solid #dcdfe6;min-height: 330px;max-height: 330px;" height="330" border>
					<!--              <el-table-column type="selection" width="55" align="center" />-->
					<el-table-column label="序号" align='center' width="50">
						<template #default="scope">
							{{ scope.$index + 1 }}
						</template>
					</el-table-column>
					<el-table-column label="id" prop="id" align="center" width="180" />
					
					<el-table-column label="档案盒号" prop="boxNo" align="center" width="180" />

					<el-table-column label="档案名称" prop="archiveName" width="180" align="center">
						<template #default="scope">
							<el-input v-model="scope.row.archiveName" style="width:130px;" maxlength="20"
								v-if="canEdit3(scope.row)"></el-input>
							<span v-else>{{ scope.row.archiveName }}</span>
						</template>
					</el-table-column>
					<el-table-column label="盘点结果" align="center" width="160">
						<template #default="scope">
							<el-select style="width:130px;" v-model="scope.row.inventoryResult"
								v-if="canEdit3(scope.row)">
								<el-option v-for="dict in filterInventoryResult(scope.row)" :key="dict.value"
									:label="dict.label" :value="dict.value">
								</el-option>
							</el-select>
							<dict-tag :options="inventory_result" :value="scope.row.inventoryResult" v-else />
						</template>
					</el-table-column>
					<el-table-column label="借阅状态" align="center" prop="borrowStatus" width="120">
						<template #default="scope">
							<el-select v-model="scope.row.borrowStatus" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in borrow_flag" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="borrow_flag" :value="scope.row.borrowStatus" v-else />
						</template>
					</el-table-column>
					<el-table-column label="档案类型" align="center" prop="archType" width="150">
						<template #default="scope">
							<el-select v-model="scope.row.archType" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in archive_type" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="archive_type" :value="scope.row.archType" v-else />
						</template>
					</el-table-column>
					<el-table-column label="档案密级" align="center" prop="confdLv" width="120">
						<template #default="scope">
							<el-select v-model="scope.row.confdLv" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in confd_lv" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="confd_lv" :value="scope.row.confdLv" v-else />
						</template>
					</el-table-column>
					<el-table-column label="保管期限" align="center" prop="keepDur" width="120">
						<template #default="scope">
							<el-select v-model="scope.row.keepDur" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in keep_dur" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="keep_dur" :value="scope.row.keepDur" v-else />
						</template>
					</el-table-column>
					<el-table-column label="介质类型" align="center" prop="medForm" width="120">
						<template #default="scope">
							<el-select v-model="scope.row.medForm" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in med_form" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="med_form" :value="scope.row.medForm" v-else />
						</template>
					</el-table-column>
					<el-table-column label="对象类型" align="center" prop="archObjType" width="120">
						<template #default="scope">
							<el-select v-model="scope.row.archObjType" placeholder="请选择" v-if="canEdit3(scope.row)">
								<el-option v-for="dict in arch_obj_type" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
							<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" v-else />
						</template>
					</el-table-column>
					<el-table-column label="对象编号" align="center" prop="archObjNo" width="180">
						<template #default="scope">
							<el-input v-model="scope.row.archObjNo" style="width:130px;" maxlength="20"
								v-if="canEdit3(scope.row)"></el-input>
							<span v-else>{{ scope.row.archObjNo }}</span>
						</template>
					</el-table-column>
					<el-table-column label="对象名称" align="center" prop="archObjName" width="180">
						<template #default="scope">
							<el-input v-model="scope.row.archObjName" style="width:130px;" maxlength="20"
								v-if="canEdit3(scope.row)"></el-input>
							<span v-else>{{ scope.row.archObjName }}</span>
						</template>
					</el-table-column>
					<el-table-column label="新增盘盈件" prop="addStatus" align="center" width="150px">
						<template #default="scope">
							{{ scope.row.addStatus == ADD_STATUS_Y ? '是' : '否' }}
						</template>
					</el-table-column>
					<el-table-column label="操作" align="center" fixed="right" width="220" v-if="form.status == '02'">
						<template #default="scope">
							<el-link type="success" icon="edit" v-hasPermi="['archive:ArchiveStocktakingTask:edit']"
								@click="handleArchive(scope.row, scope.index)">修改&nbsp;</el-link>
								&nbsp;
								<el-button type="text" icon="View" v-hasPermi="['archive:ArchiveStocktakingTask:edit']"
									@click="hideExitArcvhive(scope.row)" v-if="canEdit3(scope.row)">取消</el-button>
						</template>
					</el-table-column>
				</el-table>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="archiveCheckClose">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ArchiveStocktakingTask">
	import {
		listArchiveStocktakingTask,
		getArchiveStocktakingTask,
		delArchiveStocktakingTask,
		addArchiveStocktakingTask,
		updateArchiveStocktakingTask,
		selectBoxByTaskId,
		selectArchiveListByTaskBoxId,
		editArchiveStocktaking,
		editStocktakingBox,
		archivePDTaskResultReport,
		addArchiveStockBox
	} from "@/api/archive/ArchiveStocktakingTask";
	import {
		computed,
		getCurrentInstance,
		onBeforeUnmount,
		onMounted,
		reactive,
		ref,
		toRefs
	} from "vue";
	import {
		updateArchiveStocktaking
	} from "@/api/archive/ArchiveStocktaking";
	import {
		getPc,
		padRight
	} from "@/assets/static/js/dataUtils";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";

	const {
		proxy
	} = getCurrentInstance();
	const dateRange = ref([]);
	const ArchiveStocktakingTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const openscan = ref(false);
	const archiveBoxList = ref([]);
	const archiveCheckList = ref([]);
	const idsda = ref([]);
	const singleda = ref(true);
	const multipleda = ref(true);
	const currentBox = ref(null);

	const {
		push_status,
		execut_result,
		borrow_flag,
		save_year,
		secrecy_level,
		archive_type,
		save_extended,
		inventory_result,
		inventory_order_status,
		miss_status,
		keep_dur,
		confd_lv,
		med_form,
		arch_obj_type,
		means_type,
	} = proxy.useDict('miss_status', 'push_status', 'execut_result', 'borrow_flag', 'save_year', 'secrecy_level',
		'archive_type',
		'save_extended', 'inventory_result', 'inventory_order_status', 'keep_dur', 'confd_lv', 'means_type',
		'med_form', 'arch_obj_type');
	const cabinetOptions = ref([]);
	const areaOptions = ref([]);
	const boxOptions = ref([]);
	const data = reactive({
		form: {},
		formdah: {},
		formpdh: {
			inventoryResult: '3',
			addStatus: '1',
		},
		formda: {
			inventoryResult: '3',
			addStatus: '1',
		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			pushStatus: null,
			inventoryTime: null,
		},
	})
	const {
		queryParams,
		form,
		rules,
		formda,
		rulesda,
		rulespdh,
		queryBox,
		tempBox
	} = toRefs(data);

	//修改
	const exitMap = ref([]);
	//修改档案
	const exitMapArc = ref([]);

	const isExit = ref(false)

	// 是否能添加或修改数据
	function canEdit(row) {
		console.log("canEdit" + row.inventoryResult === INVENTORY_RESULT_ADD && (row.id == 'add' || !row.id))
		return row.inventoryResult === INVENTORY_RESULT_ADD && (row.id == 'add' || !row.id);
	}

	function canEdit2(row) {
		console.log("canEdit2" + exitMap.value.indexOf(row.id) > -1 || canEdit(row))

		return exitMap.value.indexOf(row.id) > -1 || canEdit(row);
	}

	function canEdit3(row) {
		console.log("canEdit3" + exitMapArc.value.indexOf(row.id) > -1)

		return exitMapArc.value.indexOf(row.id) > -1 || canEdit(row);
	}


	/** 查询盘点任务列表 */
	function getList() {
		loading.value = true;
		listArchiveStocktakingTask(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			ArchiveStocktakingTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	//根据档案区查询档案柜
	function changeArea(row) {
		const query = {
			archiveAreaNo: row.archAreaNo
		}
		getListArchiveCabinet(query);
	}

	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(query) {
		cabinetOptions.value = [];
		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	/** 初始化档案柜数据 */
	function initCabinetOptions() {
		// 判断档案柜的数据是否存在，存在不获取，不存在则获取
		if (cabinetOptions.value.length === 0) {
			listArchiveCabinetAll().then(response => {
				cabinetOptions.value = response.rows;
			});
		}
	};

	getListArchiveArchArea();
	getListArchiveCabinet();

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	// 档案盒选择中一行
	function handleBoxRowClick(row, column, event) {
		// 显示档案盒下的档案
		currentBox.value = row
		handleViewDetail(row.id)
	}

	function handleSelDAChange(selection) {
		idsda.value = selection.map(item => item.id);
		singleda.value = selection.length != 1;
		multipleda.value = !selection.length;
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除盘点任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delArchiveStocktakingTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	// 完成盘点单
	function finishTask(row) {
		const taskId = row.taskId;
		if (row.status == "03") {
			proxy.$modal.msgError('单据状态为【盘点完成】，不需要重复点击！');
		} else {
			const request = JSON.parse(JSON.stringify(row));
			request.status = "03"

			updateArchiveStocktakingTask(request).then(response => {
				proxy.$modal.msgSuccess("盘点完成");
				row.status = "03";
			});

		}
	}

	// 上报盘点单
	function reportTask(row) {
		if (row.status === "02") {
			proxy.$modal.msgError('单据状态为【盘点中】，上报失败！');
		} else if (row.status === "03") {
			form.value.taskId = row.taskId;
			archivePDTaskResultReport(form.value).then(response => {
				if (response.code == "200") {
					proxy.$modal.msgSuccess("盘点信息上报成功");
					getList();
				} else {
					proxy.$modal.msgError(response.msg);
				}
			}).catch(error => {
				console.error("盘点信息上报失败:", error);
			});
		}
	}

	function resetList() {
		archiveBoxList.value = [];
		archiveCheckList.value = [];
	}

	function handleView(row) {
		const taskId = row.taskId;
		getArchiveStocktakingTask(taskId).then(response => {
			form.value = response.data;
			openscan.value = true;
		});
		resetList();
		selectBoxByTaskId(taskId).then(response => {
			archiveBoxList.value = response.data;
		});
	}

	// 档案盒下的档案列表
	function handleViewDetail(taskBoxId) {
		if(!taskBoxId || taskBoxId == 'add'){
			return
		}
		archiveCheckList.value = [];
		selectArchiveListByTaskBoxId(taskBoxId).then(response => {
			if (response.code == "200") {
				archiveCheckList.value = response.data;
			} else {
				proxy.$modal.msgError(response.msg);
			}
		});
	}

	function archiveCheckClose() {
		archiveCheckList.value = []
		openscan.value = false
		getList();
	}

	// 修改档案盒内容
	function handleBoxEdit(row, index) {
		if (!canEdit2(row)) {
			exitMap.value.push(row.id)
			console.log("index=" + exitMap.value.indexOf(row.id))
			return;
		}
		console.log("row.id=" + row.id)
		let i = exitMap.value.indexOf(row.id)
		if (i !== -1) {
			exitMap.value.splice(i, 1);
		}
		const itemsToValidate = [{
				value: row.archAreaNo,
				message: "请选择档案区"
			},
			{
				value: row.colNo,
				message: "请选择档案柜"
			},
			{
				value: row.houseLocationNo,
				message: "请输入库位号"
			},
		];

		const allValid = validateAllItems(itemsToValidate);
		if (allValid) {
			if (row.id != null && row.id != 'add') {
				editStocktakingBox(row).then(response => {
					if (response.code = "200") {
						proxy.$modal.msgSuccess("盘盈档案盒修改成功!");
					} else {
						proxy.$modal.msgError(response.msg);
					}
					getList();
				});
			} else {
				row.taskId = form.value.taskId;
				addArchiveStockBox(row).then(response => {
					if (response.code = "200") {
						proxy.$modal.msgSuccess("盘盈档案盒新增成功!");
						row.id = response.data.id;
						row.boxNo = response.data.boxNo;
					} else {
						proxy.$modal.msgError(response.msg);
					}
					getList();
				});
			}
		}
		
	}

	/**
	 * 取消修改
	 */
	function hideExit(row) {
		let index = exitMap.value.indexOf(row.id)
		
		if(row.id == 'add'){
			archiveBoxList.value.forEach((item,index)=>{
				if(item.id == row.id){
					archiveBoxList.value.splice(index,1)
					return
				}
			})
		}
		
		if (index !== -1) {
			exitMap.value.splice(index, 1);
			proxy.$nextTick(() => {
				proxy.$refs.box_table.$forceUpdate()
			})
		}
	}
	
	/**
	 * 取消修改
	 */
	function hideExitArcvhive(row) {
		let index = exitMapArc.value.indexOf(row.id)
		
		if(row.id == 'add'){
			archiveCheckList.value.forEach((item,index)=>{
				if(item.id == row.id){
					archiveCheckList.value.splice(index,1)
					return
				}
			})
		}
		
		if (index !== -1) {
			exitMapArc.value.splice(index, 1);
			proxy.$nextTick(() => {
				proxy.$refs.box_table.$forceUpdate()
			})
		}
	}

	// 添加盘盈档案盒
	function handleAddArchiveBox() {
		let box = {
			inventoryResult: INVENTORY_RESULT_ADD,
			addStatus: ADD_STATUS_Y,
			missStatus: "02",
			id: 'add'
		};
		exitMap.value.push(box.id)
		archiveBoxList.value.push(box);
		proxy.$nextTick(() => {
			const scrollWrapper = proxy.$refs.box_table.$el.querySelector('.el-scrollbar__wrap')
			scrollWrapper.scrollTop = scrollWrapper.scrollHeight;
		})
	}

	// 新增盘盈档案
	function handleAddArcvhive() {
		if (currentBox.value == null || !currentBox.value.boxNo) {
			proxy.$modal.msgError("请选择一个档案盒");
			return
		}

		console.log(currentBox.value.boxNo)

		let archive = {
			id: 'add',
			taskBoxId: currentBox.value.id,
			inventoryResult: INVENTORY_RESULT_ADD,
			addStatus: ADD_STATUS_Y,
			boxNo: currentBox.value.boxNo,

		};
		archiveCheckList.value.push(archive);
		proxy.$nextTick(() => {
			const scrollWrapper = proxy.$refs.archive_table.$el.querySelector('.el-scrollbar__wrap')
			scrollWrapper.scrollTop = scrollWrapper.scrollHeight;
		})
	}

	function validateValue(value, message) {
		if (value === null || value === '' || typeof value === 'undefined') {
			proxy.$modal.msgError(message);
			return false; // 返回 false
		}
		return true; // 返回 true
	}

	function validateAllItems(items) {
		for (const {
				value,
				message
			} of items) {
			const isValid = validateValue(value, message);
			if (!isValid) {
				return false; // 如果当前项不通过验证，立即返回 false
			}
		}
		return true; // 所有项都通过验证，返回 true
	}


	// 修改档案内容
	function handleArchive(row, index) {
		if (!canEdit3(row)) {
			exitMapArc.value.push(row.id)
			console.log("index=" + exitMapArc.value.indexOf(row.id))
			return;
		}
		let i = exitMapArc.value.indexOf(row.id)
		if (i !== -1) {
			exitMapArc.value.splice(i, 1);
		}
		const itemsToValidate = [{
				value: row.archiveName,
				message: "请输入档案名称"
			},
			{
				value: row.archObjNo,
				message: "请输入对象编号"
			},
			{
				value: row.archObjName,
				message: "请输入对象名称"
			},
			{
				value: row.archObjName,
				message: "请输入对象名称"
			},
			{
				value: row.borrowStatus,
				message: "请选择借阅状态"
			},
			{
				value: row.archType,
				message: "请选择档案类型"
			},
			{
				value: row.archObjType,
				message: "请选择对象类型"
			},
			{
				value: row.medForm,
				message: "请选择介质类型"
			},
			{
				value: row.keepDur,
				message: "请选择保管期限"
			},
			{
				value: row.confdLv,
				message: "请选择密级"
			},
		];

		const allValid = validateAllItems(itemsToValidate);
		if (allValid) {
			if (row.id != null && row.id != 'add') {
				row.taskId = form.value.taskId;
				editArchiveStocktaking(row).then(response => {
					if (response.code = "200") {
						proxy.$modal.msgSuccess("盘盈档案修改成功!");
					} else {
						proxy.$modal.msgError(response.msg);
					}
					handleViewDetail(currentBox.value.id);
				});
			} else {
				row.taskId = form.value.taskId;
				editArchiveStocktaking(row).then(response => {
					if (response.code = "200") {
						proxy.$modal.msgSuccess("盘盈档案新增成功!");
					} else {
						proxy.$modal.msgError(response.msg);
					}
					handleViewDetail(currentBox.value.id);
				});
			}
		}
	}

	// 详情盘点状态-盘盈
	const INVENTORY_RESULT_ADD = '01'
	// 盘平
	const INVENTORY_RESULT_EDIT = '03'
	// 是否新增盘盈盒
	const ADD_STATUS_Y = '02'

	// 盘点结果：盘盈时只显示盘盈 01
	function filterInventoryResult(row) {
		if (row.inventoryResult === INVENTORY_RESULT_ADD) {
			return inventory_result.value.filter(dict => dict.value === INVENTORY_RESULT_ADD);
		} else {
			return inventory_result.value.filter(dict => dict.value !== INVENTORY_RESULT_ADD);
		}
	};
	getList();
</script>
